﻿'<Importaciones de Bibliotecas Generales>
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Diagnostics
Imports System.Data.Common
Imports System.Object

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data

Public Class clsAnomalias

#Region "Variables"
    ' variables de propiedad
    Private li_Codigo As Integer
    Private li_EstadoGuardar As Integer
    Private ls_Nombre As String

    ' variables generales
    Dim cGeneral As New clsGeneral
    Dim ls_Usuario As String
    Dim ls_Maquina As String
    Dim ls_Aplicacion As String
    Dim ls_Pantalla As String
#End Region

#Region "Propiedades"

    Public Property Aplicacion() As String
        Get
            Return ls_Aplicacion
        End Get
        Set(ByVal Value As String)
            ls_Aplicacion = Value
        End Set
    End Property

    Public Property Codigo() As Integer
        Get
            Return li_Codigo
        End Get
        Set(ByVal Value As Integer)
            li_Codigo = Value
        End Set
    End Property

    Public Property EstadoGuardar() As Integer
        Get
            Return li_EstadoGuardar
        End Get
        Set(ByVal Value As Integer)
            li_EstadoGuardar = Value
        End Set
    End Property

    Public Property Maquina() As String
        Get
            Return ls_Maquina
        End Get
        Set(ByVal Value As String)
            ls_Maquina = Value
        End Set
    End Property

    Public Property Nombre() As String
        Get
            Return ls_Nombre
        End Get
        Set(ByVal Value As String)
            ls_Nombre = Value
        End Set
    End Property

    Public Property Pantalla() As String
        Get
            Return ls_Pantalla
        End Get
        Set(ByVal Value As String)
            ls_Pantalla = Value
        End Set
    End Property

    Public Property Usuario() As String
        Get
            Return ls_Usuario
        End Get
        Set(ByVal Value As String)
            ls_Usuario = Value
        End Set
    End Property

#End Region

#Region "Metodos"

    Sub Borrar()
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim lb_Existe As Boolean

        ' Determina si los valores a almacenar ya existen o no
        dt = Me.ObtenerAnomalias("Numerico")
        lb_Existe = cGeneral.DataTable_Existe_en(dt, li_Codigo)
        If Not lb_Existe Then
            MessageBox.Show("Código NO está registrado, por tanto NO se puede borrar.")
            li_EstadoGuardar = -1       ' -1 = Error al grabar en la BD
        Else
            Try
                Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso_NET.My.MySettings.CnxnLitigioso")
                Dim comando As DbCommand = db.GetStoredProcCommand("uspAnomaliasDelete")
                db.AddInParameter(comando, "@Anomalia", DbType.Int16, li_Codigo)
                db.AddInParameter(comando, "@Usuario", DbType.String, ls_Usuario)
                db.AddInParameter(comando, "@Maquina", DbType.String, ls_Maquina)
                db.AddInParameter(comando, "@Aplicacion", DbType.String, ls_Aplicacion)
                db.AddInParameter(comando, "@Pantalla", DbType.String, ls_Pantalla)
                db.LoadDataSet(comando, ds, "Resultados")
                dt = ds.Tables(0)
                If dt Is Nothing Then
                    li_EstadoGuardar = -1       ' -1 = Error al grabar en la BD
                Else
                    li_EstadoGuardar = dt.Rows(0).Item(0)    ' >0 = OK , -1 = Error al grabar en la BD
                End If
                If li_EstadoGuardar = -1 Then
                    MessageBox.Show("Error al borrar en la tabla de Anomalias.", "Anomalias", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                End If
            Catch ex As Exception
                'Throw ex
                MessageBox.Show("Error al actualizar la tabla de Anomalias." & _
                ControlChars.CrLf & _
                ex.Message, "Anomalias", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                li_EstadoGuardar = -1
            End Try
        End If
    End Sub

    Sub Guardar()
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim lb_Existe As Boolean

        ' Determina si los valores a almacenar ya existen o no
        dt = Me.ObtenerAnomalias("Numerico")
        lb_Existe = cGeneral.DataTable_Existe_en(dt, li_Codigo)
        Try
            Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso_NET.My.MySettings.CnxnLitigioso")
            If Not lb_Existe Then
                Dim comando As DbCommand = db.GetStoredProcCommand("uspAnomaliasInsert")
                'db.AddInParameter(comando, "@Anomalia", DbType.Int16, li_Codigo)
                db.AddInParameter(comando, "@Nombre", DbType.String, ls_Nombre)
                db.AddInParameter(comando, "@Usuario", DbType.String, ls_Usuario)
                db.AddInParameter(comando, "@Maquina", DbType.String, ls_Maquina)
                db.AddInParameter(comando, "@Aplicacion", DbType.String, ls_Aplicacion)
                db.AddInParameter(comando, "@Pantalla", DbType.String, ls_Pantalla)
                db.LoadDataSet(comando, ds, "Resultados")
                dt = ds.Tables(0)
                If dt Is Nothing Then
                    li_EstadoGuardar = -1       ' -1 = Error al grabar en la BD
                Else
                    li_EstadoGuardar = dt.Rows(0).Item(0)    ' >0 = OK , -1 = Error al grabar en la BD
                End If
                If li_EstadoGuardar = -1 Then
                    MessageBox.Show("Error al insertar en la tabla de Anomalias.", "Anomalias", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                End If
            Else
                Dim comando As DbCommand = db.GetStoredProcCommand("uspAnomaliasUpdate")
                db.AddInParameter(comando, "@Anomalia", DbType.Int16, li_Codigo)
                db.AddInParameter(comando, "@Nombre", DbType.String, ls_Nombre)
                db.AddInParameter(comando, "@Usuario", DbType.String, ls_Usuario)
                db.AddInParameter(comando, "@Maquina", DbType.String, ls_Maquina)
                db.AddInParameter(comando, "@Aplicacion", DbType.String, ls_Aplicacion)
                db.AddInParameter(comando, "@Pantalla", DbType.String, ls_Pantalla)
                db.LoadDataSet(comando, ds, "Resultados")
                dt = ds.Tables(0)
                If dt Is Nothing Then
                    li_EstadoGuardar = -1       ' -1 = Error al grabar en la BD
                Else
                    li_EstadoGuardar = dt.Rows(0).Item(0)    ' >0 = OK , -1 = Error al grabar en la BD
                End If
                If li_EstadoGuardar = -1 Then
                    MessageBox.Show("Error al modificar en la tabla de Anomalias.", "Anomalias", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                End If
            End If
        Catch ex As Exception
            'Throw ex
            MessageBox.Show("Error al actualizar la tabla de Anomalias." & _
            ControlChars.CrLf & _
            ex.Message, "Anomalias", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            li_EstadoGuardar = -1
        End Try
    End Sub

    Public Function ObtenerAnomalias(ByVal ps_Ordenamiento As String) As DataTable
        Dim ds As New DataSet
        Dim dt As New DataTable
        Try
            Dim db As Database = DatabaseFactory.CreateDatabase("Litigioso_NET.My.MySettings.CnxnLitigioso")
            Dim comando As DbCommand = db.GetStoredProcCommand("uspAnomaliasSelect")
            db.AddInParameter(comando, "@Ordenamiento", DbType.String, ps_Ordenamiento)
            db.LoadDataSet(comando, ds, "Resultados")
            dt = ds.Tables(0)
        Catch ex As Exception
            'Throw ex
            MessageBox.Show("Error al seleccionar la lista de Anomalias." & _
            ControlChars.CrLf & _
            ex.Message, "Anomalias", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
        End Try
        Return dt
    End Function

#End Region

End Class
